import { MoreWindow,MoreDocument } from '../../type';

const toString = Object.prototype.toString;

function is(val:unknown,type:string):boolean{
    return toString.call(val) === `[object ${type}]`;
}

// 判读入参的类型

// 是否是字符串
export const isString =(val:unknown|string):boolean=>{
    return is(val,"String");
}

// 是否是数字
export const isNumber =(val:unknown|number):boolean=>{
    return is(val,'Number');
};

// 是否是方法
export const isFunction=(val:unknown|Function):boolean=>{
    return typeof val === 'function';
};

// 是否是对象
export const isObject=(val:unknown|Record<string,any>):boolean=>{
    return val !== null && is(val,'Object')
};

// 是否是window对象
export const isWindow =(val:unknown|Window):boolean=>{
    return typeof window!=='undefined' && is(val,'Window');
}

// 是否是节点
export const isElement =(val:Element):boolean=>{
    return isObject(val)&&!!val.tagName
}

// 是否是地址
export const isUrl =(path:string):boolean=>{
    const reg =
    /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/
    return reg.test(path)
};

// 是否是Promise
export const isPromise =(v:any):boolean=>{
    return v!==undefined&&v!==null&&typeof v.then === 'function' && typeof v.catch === 'function';
}

//  文件名是否是word文件
export const isWordFile =(filename:string):boolean=>{
    const wordEg = ["doc","docx","docm","dotm","dotx"]; 
    return filename&&wordEg.includes(filename);
}

// 文件名是否是excel文件
export const isExcelFile =(filename:string):boolean=>{
    const excelEg = ["xlsx","xlsb","xls","xlsm"]; 
    return filename&&excelEg.includes(filename);
}

// 文件名是否是ppt文件
export const isPptFile =(filename:string):boolean=>{
    const pptEg = ["ppt","pptx","ppsx","ppt","pps","pptm","potm","ppam","potx","ppsm"]; 
    return filename&&pptEg.includes(filename);
}

// 当前是否处于全屏状态
export const isFullScreen =():boolean=>{
    let win:MoreWindow = window;
    let doc:MoreDocument = document;
    let winFlag = win.innerHeight === win.screen.height;
    // 利用window全屏标识来判断 -- IE无效
    let isFull = win.fullScreen || doc.webkitIsFullScreen
    if (isFull === undefined) {isFull = false;}else{
        isFull = winFlag || isFull;
    }
    return isFull;
}

// 检查用户的设备是否处于暗模式
export const isDarkMode =()=>window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;


// 判断当前年份是否是闰年

export const isLeapYear=(year:string|number|null|undefined):boolean=>{
    let flag = false;
    let ryear = null;
    if(year){
        ryear = year;
    }else{
        let date=new Date;
        let y = date.getFullYear();
        ryear = y;
    };
    console.log(ryear);
    flag = ryear%100!=0&&ryear%4==0||ryear%400==0;
    return flag;
};